import { setCookies } from './util'
function resetPhone (phone) {
  var str = String(phone)
  var len = str.length
  var prev, next
  if (len >= 7) {
    prev = str.slice(-len, -8)
    next = str.slice(-3)
    str = prev + '*****' + next
  } else if (len < 7 && len >= 6) {
    prev = str.slice(-len, -4)
    next = str.slice(-2)
    str = prev + '**' + next
  }
  return str
}
function getQueryVal (variable) {
  var query = window.location.search.substring(1)
  var vars = query.split('&')
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=')
    if (pair[0] == variable) { return pair[1] }
  }
  return (false)
}
const getToken = async (code) => {
  const token = await this.$store.dispatch('admin/login/requestLogin', { code })
  console.log(token, 'token')
  if (token && token.access_token) {
    setCookies('gameToken', token.access_token)
  }
}
const shareWx = async () => {
  // const targetUrl = encodeURIComponent(location.href.replace('code=', '&ode='))
  // const targetUrl = (location.href.replace('code=', '&ode='))
  const targetUrl = encodeURI(location.href)
  console.log(targetUrl, 'targetUrl')
  const jssdk = await this.$store.dispatch('admin/userOption/share', {target_url: targetUrl})
  if (jssdk) {
    window.wx.config({
      debug: false, // 是否开启调试模式
      appId: jssdk.appId, // appid
      timestamp: jssdk.timestamp, // 时间戳
      nonceStr: jssdk.nonceStr, // 随机字符串
      signature: jssdk.signature, // 签名
      jsApiList: [
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'onMenuShareQZone'
      ] // 需要使用的JS接口列表
    })
    window.wx.ready(function () {
      // 分享给好友
      window.wx.onMenuShareAppMessage({
        title: '21世纪杯英语大会', // 分享标题
        desc: '21世纪杯英语大会重庆赛区，给你舞台！等你来战！', // 分享描述
        link: location.href.replace('code=', '&oldCode='), // 分享链接
        imgUrl: 'https://mat1.gtimg.com/cq/owens/share-icon.jpg', // 分享图标
        success: function () {},
        cancel: function () {}
      })
      // 分享到朋友圈
      window.wx.onMenuShareTimeline({
        title: '21世纪杯英语大会', // 分享标题
        desc: '21世纪杯英语大会重庆赛区，给你舞台！等你来战！',
        link: location.href.replace('code=', '&oldCode='), // 分享链接
        imgUrl: 'https://mat1.gtimg.com/cq/owens/share-icon.jpg', // 分享图标
        success: function () {},
        cancel: function () {}
      })
    })
  }
  console.log(jssdk, 'jssdk')
}
export { resetPhone, getQueryVal, getToken, shareWx }
